package com.yyt.module_main.act;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.PopupWindow;
import android.widget.SeekBar;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.google.gson.Gson;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.vondear.rxtool.RxTool;
import com.vondear.rxtool.view.RxToast;
import com.warkiz.widget.IndicatorSeekBar;
import com.warkiz.widget.OnSeekChangeListener;
import com.warkiz.widget.SeekParams;
import com.yyt.libsrc.bean.BaseCallbackBean;
import com.yyt.libsrc.bean.ConfigBean;
import com.yyt.libsrc.bean.QueryRateBean;
import com.yyt.libsrc.bean.UpdateFeeBean;
import com.yyt.libsrc.conf.Consts;
import com.yyt.libsrc.helper.LocalHelper;
import com.yyt.libsrc.helper.MsgHelper;
import com.yyt.libsrc.net.JavaParamsBuilder;
import com.yyt.libsrc.net.NetUtil;
import com.yyt.libsrc.net.SuccessCallback;
import com.yyt.libsrc.router.RouteTable;
import com.yyt.libsrc.router.RouterHelper;
import com.yyt.module_main.R;
import com.yyt.module_main.databinding.ActivityChangeRateBinding;
import com.yyt.module_main.vm.ChangeRateVM;

import top.lixb.libcommon.base.RefreshableActivity;
import top.lixb.libcommon.dialog.IOSAlertDialog;
import top.lixb.libcommon.log.L;
import top.lixb.libcommon.utils.StringUtils;

@Route(path = RouteTable.main_change_rate)
public class ChangeRateActivity extends RefreshableActivity<ActivityChangeRateBinding> {

    private ChangeRateVM mVm;
    private TextView mTvRate;
    private int mRole;
    private double mRate;
    private int mMyrole;
    private double mMyrate;

    @Override
    protected void init() {
        mVm = new ChangeRateVM();
        mRefreshContentBinding.setVm(mVm);
        setTitle("修改费率");
        mRefreshContentBinding.isb.setIndicatorTextFormat("${PROGRESS} ‰");
        mToolbarDivider.setVisibility(View.VISIBLE);
        mTvRate = mRefreshContentBinding.isb.getIndicator().getContentView().findViewById(R.id.tv_rate);
        Intent intent = getIntent();
        if (null != intent && null != intent.getExtras()) {
            mRole = intent.getExtras().getInt("role");
            mRate = intent.getExtras().getDouble("rate");
            mMyrole = intent.getExtras().getInt("myrole");
            mMyrate = intent.getExtras().getDouble("myrate");
            mVm.sid.set(intent.getExtras().getString("sid"));
            L.e("role="+ mRole +",rate="+ mRate +",myrole="+ mMyrole +",myrate="+ mMyrate);
        }


        mRefreshContentBinding.btnChangeRate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!RxTool.isFastClick(2000)) {
                    mVm.rate.set(StringUtils.doubleFormat(String.valueOf(mRefreshContentBinding.isb.getProgress() / 100d)));
                    String fee = mVm.modifyRateFee.get();
                    fee = String.valueOf(Double.valueOf(fee));
                    final String finalFee = mVm.modifyRateFee.get();
                    NetUtil.orderPost(new JavaParamsBuilder()
                            .addCmd(Consts.CMD.updatefee)
                            .addP("afterTheRate", mVm.rate.get())
                            .addP("phone", mVm.phone.get())
                            .addP("money", fee)
                            .addP("sid", mVm.sid.get())
                            .addP("operator", LocalHelper.getSid())
                            .addP("payType", "2")
                            .build(), new SuccessCallback<UpdateFeeBean>() {
                        @Override
                        public void onSuccess(final UpdateFeeBean bean) {
                            if (TextUtils.equals(bean.data.remark, "free")) {
                                MsgHelper.showToast("修改成功");
                                getData(true);
                            } else {
                                Bundle bundle = new Bundle();
                                bundle.putString("url", bean.data.payData);
                                bundle.putString("amount", finalFee);
                                bundle.putString("afterRate",bean.data.afterTheRate);
                                RouterHelper.getInstance().startAct(RouteTable.main_pay, bundle);
                            }
                        }

                    });
                }
            }
        });

        mRefreshContentBinding.isb.setOnSeekChangeListener(new OnSeekChangeListener() {
            @Override
            public void onSeeking(SeekParams seekParams) {
                int progress = seekParams.progress;
                if (null != mTvRate) {
                    mTvRate.setText(StringUtils.double4fFormat(progress/10000D));
                }
            }

            @Override
            public void onStartTrackingTouch(IndicatorSeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(IndicatorSeekBar seekBar) {

            }
        });

        mTvRate.setText(StringUtils.double4fFormat(mRefreshContentBinding.isb.getProgress()/10000d));
    }

    private void setupProgressbar(int min,int max) {
        mRefreshContentBinding.isb.setMin(min);
        mRefreshContentBinding.isb.setMax(max);
        mRefreshContentBinding.tvMin.setText(StringUtils.double4fFormat(min/10000d));
        mRefreshContentBinding.tvMax.setText(StringUtils.double4fFormat(max/10000d));
    }

    private static class ChangeRateParams{
        public String rate;
        public String sid;
        public String operator;
        public String fee;
    }

    @Override
    protected int initRefreshLayout() {
        return R.layout.activity_change_rate;
    }

    @Override
    protected void getData(boolean refresh) {
        getSetting();

    }

    private void getSetting() {
        NetUtil.sellerPost(new JavaParamsBuilder()
                .addCmd(Consts.CMD.configuration)
                .addP("name","Generation_fee")
                .build(), new SuccessCallback<ConfigBean>() {
            @Override
            public void onSuccess(ConfigBean configBean) {
                //代理费率区间
                mVm.agentRateRange.set(configBean.data.minimum+"~"+configBean.data.maximum);
                mVm.agentMinRate.set((int) (Double.valueOf(configBean.data.minimum) * 10000));
                mVm.agentMaxRate.set((int) (Double.valueOf(configBean.data.maximum) * 10000));

                NetUtil.sellerPost(new JavaParamsBuilder()
                        .addCmd(Consts.CMD.configuration)
                        .addP("name","user_fee")
                        .build(), new SuccessCallback<ConfigBean>() {
                    @Override
                    public void onSuccess(ConfigBean configBean) {
                        //普通成员区间费率
                        mVm.memberRateRange.set(configBean.data.minimum+"~"+configBean.data.maximum);
                        mVm.memberMinRate.set((int) (Double.valueOf(configBean.data.minimum) * 10000));
                        mVm.memberMaxRate.set((int) (Double.valueOf(configBean.data.maximum) * 10000));
                        NetUtil.sellerPost(new JavaParamsBuilder()
                                .addCmd(Consts.CMD.configuration)
                                .addP("name","update_rote")
                                .build(), new SuccessCallback<ConfigBean>() {
                            @Override
                            public void onSuccess(ConfigBean configBean) {
                                if (!TextUtils.isEmpty(configBean.data.value)) {
                                    mVm.modifyRateFee.set(configBean.data.value);
                                } else {
                                    if (!TextUtils.isEmpty(configBean.data.minimum)) {
                                        mVm.modifyRateFee.set(configBean.data.minimum);
                                    }
                                }
                                setupUI();
                            }
                        });
                    }
                });
            }
        });
    }

    private void setupUI() {
        if (mMyrole == 2) { //代理
            if (mRole == 1) {
                int min = (int) (mMyrate * 100);
                setupProgressbar(min,mVm.memberMaxRate.get());
            }
        } else if (mMyrole == 3) {
            if (mRole == 1) {
                setupProgressbar(mVm.memberMinRate.get(),mVm.memberMaxRate.get());
            } else if (mRole == 2) {
                setupProgressbar(mVm.agentMinRate.get(),mVm.agentMaxRate.get());
            }
        } else {
            setupProgressbar(mVm.agentMinRate.get(),mVm.memberMaxRate.get());
        }


        // TODO: 2018/8/15 获取费率
        NetUtil.sellerPost(new JavaParamsBuilder()
                .addCmd(Consts.CMD.queryRate)
                .addP("sid",mVm.sid.get())
                .build(), new SuccessCallback<QueryRateBean>() {
            @Override
            public void onSuccess(QueryRateBean queryRateBean) {
                double rate = queryRateBean.data.rate * 100;
                mRefreshContentBinding.isb.setProgress((int) rate);
                mVm.rate.set(StringUtils.double4fFormat(rate/10000));
            }

            @Override
            public void onComplete() {
                super.onComplete();
                finishGetData();
            }
        });
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        getData(true);
    }
}
